﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using BrainTechLLC.ThreadSafeObjects;
using System.Runtime.Serialization;
using System.Reflection;
using System.Diagnostics;

namespace BrainTechLLC
{
    [Serializable]
    [DataContract]
    public class Method : IEquatable<Method>
    {
        public Method() { }
        public Method(string typeName, string methodName)
        {
            TypeName = typeName;
            MethodName = methodName;
        }

        [DataMember]
        public string TypeName { get; set; }

        [DataMember]
        public string MethodName { get; set; }

        public MethodInfo FindMethod()
        {
            try
            {
                Type t = TypeProvider.GetInstance().GetType(TypeName, false);

                if (t == null)
                    throw new Exception("Type name " + TypeName + " not found.");

                MethodInfo mi = t.GetMethod(MethodName, BindingFlags.Static | System.Reflection.BindingFlags.Public);

                if (mi == null)
                    throw new Exception(String.Format("MethodName {0} of type {1} not found", MethodName, t.Name));

                return mi;
            }
            catch (Exception ex)
            {                
                Debug.WriteLine(ex.ToString());
                throw;
            }
        }

        #region IEquatable<Method> Members

        public bool Equals(Method other)
        {
            return this.MethodName == other.MethodName && this.TypeName == other.TypeName;
        }

        #endregion

        public override bool Equals(object obj)
        {
            Method m = obj as Method;

            if (m != null)
                return this.Equals(m);
            else
                return base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return MethodName.GetHashCode() + TypeName.GetHashCode();
        }
    }
}